#include "descargar.h"
#include "ui_descargar.h"
#include "../analizador.h"

Descargar::Descargar(QWidget *parent) :
	QWidget(parent), m_ui(new Ui::Descargar) {

	m_ui->setupUi(this);
	uno = false;
	dos = false;
	tres = false;
	cuatro = false;
	m_ui->descargar->setEnabled(false);

        connect(&workerThread,
                SIGNAL(updateImage()),
                SLOT(procesar()),
                Qt::QueuedConnection);
}

Descargar::~Descargar()
{
	delete m_ui;
}

void Descargar::procesar()
{


    int k;
    int progreso = 0;
    m_ui->progressBar->setValue(progreso);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    //(1) ejecutar RPC cliente (.cpp), activar servidor

    direccion = "./rpc-cliente/capturatraza_cliente "
                + m_ui->ipser->text().toStdString() + " "
                + m_ui->ipcli->text().toStdString() + " "
                + m_ui->ipser->text().toStdString() + " "
                + m_ui->puerto->text().toStdString() + " 1";
    k = system(direccion.c_str());
    m_ui->info->setText("Ejecutando Rpc Cliente");
    m_ui->info->hide();
    m_ui->info->show();
    m_ui->progressBar->setValue(progreso += 10);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    //esperar activacion
    //k = system("sleep 1");
    m_ui->info->setText("Descargando Archivo");
    m_ui->info->hide();
    m_ui->info->show();
    //(2) capturar traza cliente, descargar archivo, y finalizar captura cliente
    direccion = "sudo ./rpc-cliente/script-capturatrazaCliente.sh "
    		+ m_ui->interfaz->currentText().toStdString() + " "
                + m_ui->ipser->text().toStdString() + " "
                + m_ui->puerto->text().toStdString() + " "
                + m_ui->nombarchivo->text().toStdString() + "";
    k = system(direccion.c_str());
    m_ui->info->setText("Capturando Traza Cliente");
    m_ui->info->hide();
    m_ui->info->show();
    m_ui->progressBar->setValue(progreso += 10);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    //(3) ejecutar RPC, para finalizar servidor
    direccion = "./rpc-cliente/capturatraza_cliente "
                + m_ui->ipser->text().toStdString() + " "
                + m_ui->ipcli->text().toStdString() + " "
                + m_ui->ipser->text().toStdString() + " "
                + m_ui->puerto->text().toStdString() + " 2";
    k = system(direccion.c_str());
    m_ui->info->setText("Ejecutando Rpc Finalizando Servidor");
    m_ui->info->hide();
    m_ui->info->show();
    m_ui->progressBar->setValue(progreso += 15);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();
    //(4) aplicar filtros en cliente
    direccion = "./rpc-cliente/script-filtrosCliente.sh "
                + m_ui->ipcli->text().toStdString() + " "
                + m_ui->ipser->text().toStdString() + " "
                + m_ui->puerto->text().toStdString() + "";
    k = system(direccion.c_str());
    m_ui->info->setText("Ejecutando Filtros Sobre Trazas");
    m_ui->info->hide();
    m_ui->info->show();
    m_ui->progressBar->setValue(progreso += 15);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    //esperar desactivacion y filtrado de trazas
    k = system("sleep 3");
    m_ui->info->setText("Esperando Filtrado de Trazas");
    m_ui->info->hide();
    m_ui->info->show();
    m_ui->progressBar->setValue(progreso += 10);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    //(5) descargar trazas
    direccion = "wget http://" + m_ui->ipser->text().toStdString() + ":"
                + m_ui->puerto->text().toStdString()
                + "/rpc-servidor/tr-srv/acks1.txt";
    k = system(direccion.c_str());
    direccion = "wget http://" + m_ui->ipser->text().toStdString() + ":"
                + m_ui->puerto->text().toStdString()
                + "/rpc-servidor/tr-srv/acks-dup1.txt";
    k = system(direccion.c_str());
    direccion = "wget http://" + m_ui->ipser->text().toStdString() + ":"
                + m_ui->puerto->text().toStdString()
                + "/rpc-servidor/tr-srv/servidor-ACKs.txt";
    k = system(direccion.c_str());
    direccion = "wget http://" + m_ui->ipser->text().toStdString() + ":"
                + m_ui->puerto->text().toStdString()
                + "/rpc-servidor/tr-srv/servidor-DATApkts.txt";
    k = system(direccion.c_str());
    m_ui->info->setText("Descargando Trazas");
    m_ui->info->hide();
    m_ui->info->show();
    m_ui->progressBar->setValue(progreso += 10);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    k= system("cp acks1.txt acks-dup1.txt servidor-ACKs.txt servidor-DATApkts.txt datos");
    m_ui->info->setText("Copiando Data");
    m_ui->info->hide();
    m_ui->info->show();
    m_ui->progressBar->setValue(progreso += 10);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    k= system("rm acks1.txt acks-dup1.txt servidor-ACKs.txt servidor-DATApkts.txt");
    m_ui->info->setText("Borrando Temporales");
    m_ui->info->hide();
    m_ui->info->show();
    m_ui->progressBar->setValue(progreso += 10);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    //generar calculos
    analizador();
    m_ui->info->setText("Analizando Data Generando Graficas");
    m_ui->info->hide();
    m_ui->info->show();
    sleep(5);
    m_ui->progressBar->setValue(progreso += 10);
    m_ui->progressBar->hide();
    m_ui->progressBar->show();

    m_ui->info->setText("Ver Resultados en Graficas");
    m_ui->info->hide();
    m_ui->info->show();

    workerThread.terminate();
}

void Descargar::changeEvent(QEvent *e) {
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void Descargar::updatevalidator1() {
    if (m_ui->ipser->text().size() > 0)
        uno = true;
    else
        uno = false;

    if ((uno) && (dos) && (tres) && (cuatro))
        m_ui->descargar->setEnabled(true);
    else
        m_ui->descargar->setEnabled(false);
}
void Descargar::updatevalidator2() {
    if (m_ui->ipcli->text().size() > 0)
        dos = true;
    else
        dos = false;

    if ((uno) && (dos) && (tres) && (cuatro))
        m_ui->descargar->setEnabled(true);
    else
        m_ui->descargar->setEnabled(false);
}
void Descargar::updatevalidator3() {
    if (m_ui->nombarchivo->text().size() > 0)
        tres = true;
    else
        tres = false;

    if ((uno) && (dos) && (tres) && (cuatro))
        m_ui->descargar->setEnabled(true);
    else
        m_ui->descargar->setEnabled(false);
}
void Descargar::updatevalidator4() {
    if (m_ui->puerto->text().size() > 0)
        cuatro = true;
    else
        cuatro = false;

    if ((uno) && (dos) && (tres) && (cuatro))
        m_ui->descargar->setEnabled(true);
    else
        m_ui->descargar->setEnabled(false);
}
void Descargar::on_descargar_clicked()
{
    procesar();
}

